<?php

namespace app\admin\controller\project;

use app\admin\controller\Base;
use think\Db;

/**
 * 用户组管理
 * Class GroupController
 * @package app\admin\controller\auth
 */
class Projectadmin extends Base
{
    protected $beforeActionList = [
        'first' =>  ['except'=>'msg'],
    ];
    protected $project_user = [];

    protected function first()
    {
        $post = $this->request->param();
        $info = Db::name('project_user')
            ->where('project_id', $post['project_id'])
            ->where('uid', session('uid'))
            ->find();
        if (!$info) {
            $this->error('你没有权限进入本模块');
        } else {
            $this->project_user = $info;
            $this->assign('project_user',$info['user_type']);
            $this->assign('project_id', $post['project_id']);
            $this->assign('project_model', config('param.project_model'));
        }
    }


    /**
     * 主页面
     * @return mixed
     */
    public function msg()
    {
        return view();
    }
    public function index()
    {
        return view();
    }

    public function board()
    {
        return view();
    }

    public function project_user_lists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        $map[] = ['project_user.project_id', '=', $post['project_id']];
        $data['count'] = Db::view('project_user', 'id,project_id,uid')
            ->view('admin' , 'nickname', 'project_user.uid=admin.id')
            ->view('auth_group_access' , 'group_id', 'auth_group_access.uid=admin.id')
            ->view('auth_group','title','auth_group.id=auth_group_access.group_id')
            ->where($map)
            ->count();
        $data['data'] =  Db::view('project_user', 'id,project_id,uid')
            ->view('admin' , 'nickname', 'project_user.uid=admin.id')
            ->view('auth_group_access' , 'group_id', 'auth_group_access.uid=admin.id')
            ->view('auth_group','title','auth_group.id=auth_group_access.group_id')
            ->where($map)
            //->fetchSql(true)
            ->order('id desc')->page(input("param.page"), input('param.limit'))->select();
        //echo $data['data'];
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    public function getprojectinfo()
    {
        $info = array();
        $info['model_1']['sum'] = DB::name('stories')
            ->where('project_id', input('param.project_id'))
           // ->where('e_time', '<=', time())
            ->where('model_id', 1)
            ->where('delete_time',0)
            ->cache(120)
            ->count();//总数
        $info['model_1']['out'] = DB::name('stories')
            ->where('project_id', input('param.project_id'))
            ->where('status', '<>', 5)
            ->where('e_time', '<=', time())
            ->where('model_id', 1)
            ->where('delete_time',0)
            ->cache(120)
            ->count();//超时
        $info['model_1']['no'] = DB::name('stories')
            ->where('project_id', input('param.project_id'))
            ->where('status', '<>', 5)
            ->where('model_id', 1)
            ->where('delete_time',0)
            ->cache(120)
            ->count();//未完成
        $info['model_1']['work'] = DB::name('stories')
            ->where('project_id', input('param.project_id'))
            ->where('status', '=', 3)
            ->where('model_id', 1)
            ->where('delete_time',0)
            ->cache(120)
            ->count();//处理中
        $info['model_1']['no_work'] = DB::name('stories')
            ->where('project_id', input('param.project_id'))
            ->where('status', '<', 3)
            ->where('model_id', 1)
            ->where('delete_time',0)
            ->cache(120)
            ->count();//未处理
        $info['model_1']['ok'] = DB::name('stories')
            ->where('project_id', input('param.project_id'))
            ->where('status', '=', 5)
            ->where('model_id', 1)
            ->where('delete_time',0)
            ->cache(120)
            ->count();//完成
        $info['model_1']['p_time'] = DB::name('stories')
            ->where('project_id', input('param.project_id'))
            ->where('model_id', 1)
            ->where('delete_time',0)
            ->cache(120)
            ->sum('p_time');
        $info['model_1']['w_time'] = DB::name('stories')
            ->where('project_id', input('param.project_id'))
            ->where('model_id', 1)
            ->where('delete_time',0)
            ->cache(120)
            ->sum('w_time');
        //2
        $info['model_2']['sum'] = DB::name('stories')
            ->where('project_id', input('param.project_id'))
           // ->where('e_time', '<=', time())
            ->where('model_id', 1)
            ->where('delete_time',0)
            ->cache(120)
            ->count();//总数
        $info['model_2']['out'] = DB::name('stories')
            ->where('project_id', input('param.project_id'))
            ->where('status', '<>', 5)
            ->where('e_time', '<=', time())
            ->where('model_id', 2)
            ->where('delete_time',0)
            ->cache(120)
            // ->fetchSql(true)
            ->count();//超时
        $info['model_2']['no'] = DB::name('stories')
            ->where('project_id', input('param.project_id'))
            ->where('status', '<>', 5)
            ->where('model_id', 2)
            ->where('delete_time',0)
            ->cache(120)
            ->count();//未完成
        $info['model_2']['work'] = DB::name('stories')
            ->where('project_id', input('param.project_id'))
            ->where('status', '=', 3)
            ->where('model_id', 2)
            ->where('delete_time',0)
            ->cache(120)
            ->count();//处理中
        $info['model_2']['no_work'] = DB::name('stories')
            ->where('project_id', input('param.project_id'))
            ->where('status', '<', 3)
            ->where('model_id', 2)
            ->where('delete_time',0)
            ->cache(120)
            ->count();//未处理
        $info['model_2']['ok'] = DB::name('stories')
            ->where('project_id', input('param.project_id'))
            ->where('status', '=', 5)
            ->where('model_id', 2)
            ->cache(120)
            ->count();//完成
        $info['model_2']['p_time'] = DB::name('stories')
            ->where('project_id', input('param.project_id'))
            ->where('model_id', 2)
            ->where('delete_time',0)
            ->cache(120)
            ->sum('p_time');
        $info['model_2']['w_time'] = DB::name('stories')
            ->where('project_id', input('param.project_id'))
            ->where('model_id', 2)
            ->where('delete_time',0)
            ->cache(120)
            ->sum('w_time');
        //3
        $info['model_3']['sum'] = DB::name('stories')
            ->where('project_id', input('param.project_id'))
            ->where('e_time', '<=', time())
            ->where('model_id', 1)
            ->where('delete_time',0)
            ->cache(120)
            ->count();//总数
        $info['model_3']['out'] = DB::name('stories')
            ->where('project_id', input('param.project_id'))
            ->where('status', '<>', 5)
            ->where('e_time', '<=', time())
            ->where('model_id', 3)
            ->where('delete_time',0)
            ->cache(120)
            ->count();//超时
        $info['model_3']['no'] = DB::name('stories')
            ->where('project_id', input('param.project_id'))
            ->where('status', '<>', 5)
            ->where('model_id', 3)
            ->where('delete_time',0)
            ->cache(120)
            ->count();//未完成
        $info['model_3']['work'] = DB::name('stories')
            ->where('project_id', input('param.project_id'))
            ->where('status', '=', 3)
            ->where('model_id', 3)
            ->where('delete_time',0)
            ->cache(120)
            ->count();//处理中
        $info['model_3']['no_work'] = DB::name('stories')
            ->where('project_id', input('param.project_id'))
            ->where('status', '<', 3)
            ->where('model_id', 3)
            ->where('delete_time',0)
            ->cache(120)
            ->count();//未处理
        $info['model_3']['ok'] = DB::name('stories')
            ->where('project_id', input('param.project_id'))
            ->where('status', '=', 5)
            ->where('model_id', 3)
            ->where('delete_time',0)
            ->cache(120)
            ->count();//完成
        $info['model_3']['p_time'] = DB::name('stories')
            ->where('project_id', input('param.project_id'))
            ->where('model_id', 3)
            ->where('delete_time',0)
            ->cache(120)
            ->sum('p_time');
        $info['model_3']['w_time'] = DB::name('stories')
            ->where('project_id', input('param.project_id'))
            ->where('model_id', 3)
            ->where('delete_time',0)
            ->cache(120)
            ->sum('w_time');
        //sum
        $info['sum']['sum'] = $info['model_1']['sum'] + $info['model_2']['sum'] + $info['model_3']['sum'];//总数
        $info['sum']['out'] = $info['model_1']['out'] + $info['model_2']['out'] + $info['model_3']['out'];//超时
        $info['sum']['no'] = $info['model_1']['no'] + $info['model_2']['no'] + $info['model_3']['no'];//未完成
        $info['sum']['work'] = $info['model_1']['work'] + $info['model_2']['work'] + $info['model_3']['work'];//处理中
        $info['sum']['no_work'] = $info['model_1']['no_work'] + $info['model_2']['no_work'] + $info['model_3']['no_work'];//未处理
        $info['sum']['ok'] = $info['model_1']['ok'] + $info['model_2']['ok'] + $info['model_3']['ok'];//完成
        $info['sum']['p_time'] = $info['model_1']['p_time'] + $info['model_2']['p_time'] + $info['model_3']['p_time'];//完成
        $info['sum']['w_time'] = $info['model_1']['w_time'] + $info['model_2']['w_time'] + $info['model_3']['w_time'];//完成
        $project = DB::name('project')->where('id', input('param.project_id'))->find();
        $project['s_time'] == 0 ? $project['s_time'] = '--' : $project['s_time'] = date('Y-m-d', $project['s_time']);
        $project['e_time'] == 0 ? $project['e_time'] = '--' : $project['e_time'] = date('Y-m-d', $project['e_time']);
        $project['info'] = DB::name('infoview')->where('id', $project['info_id'])->value('info');
        $project_status = config('param.project_status');
        $project['status'] = $project_status[$project['status']];
        $info['project'] = $project;
        $data['data'] = $info;

        if ($data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

}